package com.wongoing.bus.util;

import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.ServletContextAware;

import com.wongoing.sys.service.SysConfigService;
import com.wongoing.util.LogService;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

/**
 * 功能说明：初始化监听器类，用于初始化全局数据
 * 修改说明：
 * @author zhenglibing
 * @date 2017年11月2日 下午4:35:57
 * @version 0.1
 */
public class StartupListener implements ApplicationContextAware, ServletContextAware, InitializingBean{

	private ApplicationContext applicationContext;
	private ServletContext servletContext;
	
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		System.out.println("1 => StartupListener.setApplicationContext");
		this.applicationContext = applicationContext;
	}

	public void setServletContext(ServletContext servletContext) {
		System.out.println("2 => StartupListener.setServletContext");
		this.servletContext = servletContext;
		try {
			EhCacheManagerFactoryBean cacheManagerFactory = applicationContext.getBean(EhCacheManagerFactoryBean.class);
			
			Cache cache = cacheManagerFactory.getObject().getCache("baseData");
			//Element elementProvinces = new Element("provinces", provinces);
			//cache.put(elementProvinces);
			
			// 初始化系统参数配置
			SysConfigService sysConfigService = applicationContext.getBean(SysConfigService.class);
			sysConfigService.initAllConfig();
			
		}
		catch(Exception ex) {
			LogService.getInstance().error("StartupListener.setServletContext初始化数据异常：" + ex.getMessage(), ex);
		}
		
//		this.servletContext.setAttribute("company", "青岛弯弓信息技术有限公司");
	}

	public void afterPropertiesSet() throws Exception {
		System.out.println("3 => StartupListener.afterPropertiesSet");
	}
}
